package com.client.qiuqiu.adapters;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.client.qiuqiu.Chatmessage;
import com.client.qiuqiu.databinding.ItemContainerReceivedMessageBinding;
import com.client.qiuqiu.databinding.ItemContainnerSendMessageBinding;


import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private  final List<Chatmessage> chatmessages;
    private final String userID;

    public static final int VIEW_TYPE_SEND = 1;
    public static final int VIEW_TYPE_RECEIVED = 2;

    public ChatAdapter(List<Chatmessage> chatmessages, String userID) {
        this.chatmessages = chatmessages;
        this.userID = userID;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_SEND){
            return new SendMessageViewHolder(
                    ItemContainnerSendMessageBinding.inflate(
                            LayoutInflater.from(parent.getContext()),
                            parent,
                            false
                    )
            );
        }else {
            return new ReceivedMessageViewHolder(
                    ItemContainerReceivedMessageBinding.inflate(
                            LayoutInflater.from(parent.getContext()),
                            parent,
                            false
                    )
            );
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(getItemViewType(position) == VIEW_TYPE_SEND){
            ((SendMessageViewHolder) holder).setData(chatmessages.get(position));
        }else {
            ((ReceivedMessageViewHolder) holder).setData(chatmessages.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return chatmessages.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (chatmessages.get(position).senderID.equals(userID)){

            return VIEW_TYPE_SEND;
        }else {
            return VIEW_TYPE_RECEIVED;
        }
    }


    static class SendMessageViewHolder extends RecyclerView.ViewHolder{
        private final ItemContainnerSendMessageBinding binding;

        SendMessageViewHolder(ItemContainnerSendMessageBinding itemContainnerSendMessageBinding){
            super(itemContainnerSendMessageBinding.getRoot());
            binding = itemContainnerSendMessageBinding;
        }

        void setData(Chatmessage chatmessage){
            binding.textMessage.setText(chatmessage.message);
        }
    }

    static class ReceivedMessageViewHolder extends RecyclerView.ViewHolder{
        private final ItemContainerReceivedMessageBinding binding;

        ReceivedMessageViewHolder(ItemContainerReceivedMessageBinding itemContainerReceivedMessageBinding){
            super(itemContainerReceivedMessageBinding.getRoot());
            binding = itemContainerReceivedMessageBinding;
        }

        void setData(Chatmessage chatmessage){
            binding.textMessage.setText(chatmessage.message);
        }
    }
}
